package com.learn.project2.service.wx.catalog;

import com.learn.project2.bean.MarketCategory;
import com.learn.project2.bean.MarketCategoryExample;
import com.learn.project2.mapper.MarketCategoryMapper;
import com.learn.project2.model.vo.wx.catalog.CatalogCurrentVO2;
import com.learn.project2.model.vo.wx.catalog.CatalogIndexVO2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 曹源
 * @version 1.0
 * @description: 微信小程序 /wx/catalog的接口的方法实现
 * @date 2022/1/10 14:40
 */
@Service
public class CatalogServiceIml2 implements CatalogService2 {
    @Autowired
    MarketCategoryMapper marketCategoryMapper;

    /**
     * @description: 微信小程序 /wx/catalog/current的接口的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-10 22:07
     */
    @Override
    public CatalogCurrentVO2 selectById(Integer id) {
        CatalogCurrentVO2 catalogCurrentVO = new CatalogCurrentVO2();

        //查询主键Id的currentCategory
        MarketCategory marketCategory = marketCategoryMapper.selectByPrimaryKey(id);
        catalogCurrentVO.setCurrentCategory(marketCategory);

        //查询pid的currentSubCategory
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(id).andDeletedEqualTo(false);
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        catalogCurrentVO.setCurrentSubCategory(marketCategories);
        return catalogCurrentVO;
    }

    /**
     * @description: 微信小程序 /wx/catalog/index的接口的方法实现
     * @param:
     * @return:
     * @author 曹源
     * @date: 2022-01-11 09:11
     */
    @Override
    public CatalogIndexVO2 selectIndex() {
        CatalogIndexVO2 catalogIndexVO = new CatalogIndexVO2();
        //查询出pid=0的categoryList
        MarketCategoryExample marketCategoryExample = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria = marketCategoryExample.createCriteria();
        criteria.andPidEqualTo(0).andDeletedEqualTo(false).andLevelEqualTo("L1");
        List<MarketCategory> marketCategories = marketCategoryMapper.selectByExample(marketCategoryExample);
        catalogIndexVO.setCategoryList(marketCategories);
        //查询出pid=0的categoryList的第一个的值为currentCategory
        catalogIndexVO.setCurrentCategory(marketCategories.get(0));
        //查询出pid=currentCategory.id的值作为currentSubCategory
        MarketCategoryExample marketCategoryExample1 = new MarketCategoryExample();
        MarketCategoryExample.Criteria criteria1 = marketCategoryExample1.createCriteria();
        criteria1.andPidEqualTo(marketCategories.get(0).getId()).andDeletedEqualTo(false).andLevelEqualTo("L2");
        List<MarketCategory> marketCategories1 = marketCategoryMapper.selectByExample(marketCategoryExample1);
        catalogIndexVO.setCurrentSubCategory(marketCategories1);
        return catalogIndexVO;
    }
}
